package com.css.ids.server.service.dao;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import com.css.apps.base.common.Constants;
import com.css.apps.base.org.model.SOrgPost;
import com.css.apps.base.post.model.SPost;
import com.css.apps.base.user.model.SUser;
import com.css.db.query.QueryCache;

public class IdsPostDao {
	
    public static SPost getPost(String postId) {
        return QueryCache.get(SPost.class, postId);
    }
    
    @SuppressWarnings("unchecked")
	public static List<SPost> listPostsByIds(Collection<String> postIds) {
    	return QueryCache.idToObj(SPost.class, new ArrayList<>(postIds));
    }
	
    @SuppressWarnings("unchecked")
	public static List<SPost> getPostByUserId(String userId) {
        SUser user = QueryCache.get(SUser.class, userId);
        if (user == null) return null;
        return user.getPostList().getList();
    }
    
	@SuppressWarnings("unchecked")
    public static List<SOrgPost> queryOrgPostsByName(String postName) {
        String hql = " from SOrgPost a where a.postId in (select b.uuid from SPost b where b.name = :postName) ";
        List<SOrgPost> orgPostList = new QueryCache(hql).setParameter("postName", postName).list();
        return orgPostList;
    }

    @SuppressWarnings("unchecked")
    public static List<SPost> queryPostsByUser(String userId) {
        String hql = "select postId from SUserPost where userId = :userId";
        QueryCache qc = new QueryCache(hql).setParameter("userId", userId);
        return qc.idToObject(SPost.class, qc.list());
    }

    @SuppressWarnings("unchecked")
    public static List<SPost> queryPostsByRole(String roleId) {
        String hql = "select distinct a.postId from SPostRole a where a.roleId = :roleId";
        QueryCache qc = new QueryCache(hql).setParameter("roleId", roleId);
        return qc.idToObject(SPost.class, qc.list());
    }

    @SuppressWarnings("unchecked")
	public static List<SPost> queryPostsByOrg(String orgId) {
        String hql = "select distinct a.postId from SOrgPost a where a.orgId = :orgId ";
        QueryCache qc = new QueryCache(hql).setParameter("orgId", orgId);
        return qc.idToObject(SPost.class, qc.list());
    }

	@SuppressWarnings("unchecked")
    public static List<SPost> listAllPost(boolean isGroup) {
    	StringBuffer sb = new StringBuffer("from SPost");
    	if(isGroup) {
    		sb.append(" where groupFlag = ").append(Constants.YES).append(" order by orderNum asc");
    	}
    	QueryCache qc = new QueryCache(sb.toString());
        return qc.list();
    }

	@SuppressWarnings("unchecked")
	public static List<SOrgPost> queryOrgPostsByNames(String... postNames) {
		StringBuilder pn = new StringBuilder();
		for (String name : postNames) {
			pn.append("b.name = '").append(name).append("' or ");
		}
		String hql = "from SOrgPost a";
		int index = pn.lastIndexOf("or ");
		if (index != -1) {
			hql = "from SOrgPost a where a.postId in (select b.uuid from SPost b where " + pn.substring(0, index) + ")";
		}
		return new QueryCache(hql).list();
	}

	@SuppressWarnings("unchecked")
	public static List<String> listPostsByNames(String[] postNames) {
		return new QueryCache("select uuid from SPost where name in (:name)").setParameter("name", postNames).list();
	}
	
}
